package com.lf.web.service.system.group;

import com.lf.data.ClusterUtil;
import com.lf.web.service.ResponseCode;
import com.lf.web.service.ResponseInfo;
import com.lf.web.service.ServiceSupport;
import org.apache.log4j.Logger;

import java.util.Date;
import java.util.HashMap;

public class GroupAddService extends ServiceSupport {

    private static final Logger LOGGER = Logger.getLogger(GroupAddService.class);
    private GroupDAO groupdao;

    public void setGroupdao(GroupDAO groupdao) {
        this.groupdao = groupdao;
    }

    public String execute() {
        String type = form.getString("type");
        if ("save".equals(type)) {
            return save();
        } else {
            return add();
        }
    }

    private String add() {
        context.put("check", ClusterUtil.getCheck(Group.class));
        return ADD;
    }

    private String save() {
        Group group = new Group();
        boolean result = form.getCluster(group);
        LOGGER.debug("添加组：" + group);
        if (result) {
            String id = group.setId();
            group.put(Group.LIMIT, new HashMap());
            group.put(Group.CREATED, new Date());
            groupdao.insert(group);
            ResponseInfo msg = new ResponseInfo(ResponseCode.FINISH);
            msg.put("id", id);
            out(msg);
        } else {
            error(404);
            LOGGER.error("非法添加组：" + group);
        }
        return NONE;
    }
}
